home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Mud 4.0 / lpmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-17  |  5.8 KB  |  272 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <setjmp.h>
  5. #include "lint.h"
  6. #include "interpret.h"
  7. #include "config.h"
  8. #include "object.h"
  9. #include "lex.h"
  10. #include "rc.h"
  11.  
  12. #include <quickdraw.h>
  13. #include <memory.h>
  14. #include <cursorctl.h>
  15. #include <mac.h>
  16.  
  17. void InitConsole();
  18.  
  19. int slow_shut_down_to_do;
  20. int d_flag = 0;    /* Run with debug */
  21. int t_flag = 0;    /* Disable heart beat and reset */
  22. int e_flag = 0;    /* Load empty, without castles. */
  23. int comp_flag = 0; /* Trace compilations */
  24. long time_to_swap; /* marion - invocation parameter */
  25.  
  26. #ifdef YYDEBUG
  27. extern int yydebug;
  28. #endif
  29.  
  30. int port_number;
  31. char *reserved_area;    /* reserved for malloc() */
  32. struct svalue const0, const1;
  33.  
  34. double consts[5];
  35.  
  36. extern jmp_buf error_recovery_context;
  37. extern int error_recovery_context_exists;
  38.  
  39. extern struct object *master_ob;
  40.  
  41. struct wiz_list *back_bone_uid;
  42.  
  43. static void start_ip_demon();
  44.  
  45. int lpmain(void)
  46. {
  47.     int i;
  48.     char *p;
  49.     extern int num_player;
  50.     struct svalue simulf;
  51.     extern FILE *swap_file;
  52.     extern int current_time;
  53.     extern int new_name_count;
  54.     extern void clear_defines();
  55.     extern void init_compiler(void);
  56.     extern void flush_instr_cache(void);
  57.     extern int slow_shut_down_to_do;
  58.     extern int game_is_being_shut_down;
  59.     extern struct program *prog;
  60.     extern struct object *obj_list;
  61.     extern struct object *previous_ob;
  62.     extern struct object *command_giver;
  63.     extern struct program *current_prog;
  64.     extern struct object *current_object;
  65.     extern struct object *obj_list_destruct;
  66.     extern struct object *current_interactive;
  67.     extern struct interactive **all_players;
  68.  
  69.  
  70. #ifndef COMPAT_MODE
  71.     struct svalue *ret;
  72. #endif
  73.  
  74.     prog = NULL;
  75.     num_player = 0;
  76.     master_ob = NULL;
  77.     obj_list = NULL;
  78.     swap_file = NULL;
  79.     previous_ob = NULL;
  80.     new_name_count = 0;
  81.     current_prog = NULL;
  82.     current_object = NULL;
  83.     obj_list_destruct = NULL;
  84.     current_interactive = NULL;
  85.     game_is_being_shut_down = 0;
  86.     slow_shut_down_to_do = 0;
  87.     command_giver = 0;
  88.     for (i = 0; i < MAX_PLAYERS; i++)
  89.         all_players[i] = NULL;
  90.  
  91.     flush_instr_cache();
  92.     clear_defines();
  93.     init_otable();
  94.     init_strings();
  95.     init_compiler();
  96.  
  97.     InitConsole();
  98.     printf("LPC Interpreter 3.1.2+.\nCompiled on ");
  99.     printf(__DATE__);
  100.     printf(" ");
  101.     printf(__TIME__);
  102.     printf(".\n");
  103.     fflush(stdout);
  104.     InitCursorCtl(NULL);
  105.  
  106.     const0.type = T_NUMBER; const0.u.number = 0;
  107.     const1.type = T_NUMBER; const1.u.number = 1;
  108.     /*
  109.      * Check that the definition of EXTRACT_UCHAR() is correct.
  110.      */
  111.     p = (char *)&i;
  112.     *p = -10;
  113.     if (EXTRACT_UCHAR(p) != 0x100 - 10) {
  114.         fprintf(stderr, "Bad definition of EXTRACT_UCHAR() in config.h.\n");
  115.         LPExit(1);
  116.     }
  117. //    Initialize randomizer
  118.     qd.randSeed = TickCount();
  119.  
  120.     init_num_args();
  121.     reset_machine(1);
  122.  
  123.     current_time = get_current_time();;
  124.     if (RESERVED_SIZE > 0)
  125.         reserved_area = xalloc(RESERVED_SIZE);
  126.     for (i=0; i < sizeof consts / sizeof consts[0]; i++)
  127.         consts[i] = exp(- i / 900.0);
  128.  
  129.     if (setjmp(error_recovery_context)) {
  130.         clear_state();
  131.         add_message("Anomaly in the fabric of world space.\n");
  132.     } else {
  133.         error_recovery_context_exists = 1;
  134.         simulf.type = T_STRING;
  135.         simulf.string_type = STRING_CONSTANT;
  136.         simulf.u.string = SIMUL_EFUN;
  137.         get_simul_efun(&simulf);
  138.         master_ob = load_object(MASTER_OBJECT,0, 0);
  139.     }
  140.     error_recovery_context_exists = 0;
  141.     if (master_ob == 0) {
  142.         fprintf(stderr, "The file %s must be loadable.\n",MASTER_OBJECT);
  143.         LPExit(1);
  144.     }
  145.     /*
  146.      * Make sure master_ob is never made a dangling pointer.
  147.      * Look at apply_master_ob() for more details.
  148.      */
  149.     add_ref(master_ob, "main");
  150. #ifndef COMPAT_MODE
  151.     ret = apply_master_ob("get_root_uid", 0);
  152.     if (ret == 0 || ret->type != T_STRING) {
  153.         fprintf(stderr, "get_root_uid() in %s does not work\n", MASTER_OBJECT);
  154.         LPExit(1);
  155.     }
  156.     master_ob->user = add_name(ret->u.string);
  157.     master_ob->eff_user = master_ob->user;
  158.     ret = apply_master_ob("get_bb_uid", 0);
  159.     if (ret == 0 || ret->type != T_STRING) {
  160.         fprintf(stderr, "get_bb_uid() in %s does not work\n", MASTER_OBJECT);
  161.         LPExit(1);
  162.     }
  163.     back_bone_uid = add_name(ret->u.string);
  164. #endif
  165.     if (game_is_being_shut_down)
  166.         LPExit(1);
  167.     load_wiz_file();
  168.     set_inc_list(apply_master_ob("define_include_dirs", 0));
  169. #ifdef COMPAT_MODE
  170.     load_first_objects();
  171.     (void)apply_master_ob("epilog", 0);
  172. #else
  173.     preload_objects(e_flag);
  174. #endif
  175.     backend();
  176.  
  177.     return 0;
  178. }
  179.  
  180. char *string_copy(str)
  181.     char *str;
  182. {
  183.     char *p;
  184.  
  185.     p = xalloc(strlen(str)+1);
  186.     (void)strcpy(p, str);
  187.     return p;
  188. }
  189.  
  190. /*VARARGS1*/
  191. void debug_message(a, b, c, d, e, f, g, h, i, j)
  192.     char *a;
  193.     int b, c, d, e, f, g, h, i, j;
  194. {
  195.     fprintf(stderr, a, b, c, d, e, f, g, h, i, j);
  196.     fflush(stderr);
  197. }
  198.  
  199. void debug_message_svalue(v)
  200.     struct svalue *v;
  201. {
  202.     if (v == 0) {
  203.     debug_message("<NULL>");
  204.     return;
  205.     }
  206.     switch(v->type) {
  207.     case T_NUMBER:
  208.     debug_message("%d", v->u.number);
  209.     return;
  210.     case T_STRING:
  211.     debug_message("\"%s\"", v->u.string);
  212.     return;
  213.     case T_OBJECT:
  214.     debug_message("OBJ(%s)", v->u.ob->name);
  215.     return;
  216.     case T_LVALUE:
  217.     debug_message("Pointer to ");
  218.     debug_message_svalue(v->u.lvalue);
  219.     return;
  220.     default:
  221.     debug_message("<INVALID>\n");
  222.     return;
  223.     }
  224. }
  225.  
  226. #ifdef malloc
  227. #undef malloc
  228. #endif
  229.  
  230. void xfree(char *p)
  231. {
  232.     DisposPtr(p);
  233. }
  234.  
  235. char *xalloc(int size)
  236. {
  237.     char *p;
  238.     extern void LPApplicZone(void);
  239.     extern void LPRuntimeZone(void);
  240.  
  241.     LPRuntimeZone();
  242.     p = NewPtr(size);
  243.     LPApplicZone();
  244.     if (p == 0) {
  245.         fprintf(stderr, "Totally out of MEMORY.\n");
  246.         fflush(stderr);
  247.         (void)dump_trace(0);
  248.         LPExit(2);
  249.     }
  250.     return p;
  251. }
  252.  
  253. char *xrealloc(char *p, unsigned int size)
  254. {
  255.     char *p2;
  256.     extern void LPApplicZone(void);
  257.     extern void LPRuntimeZone(void);
  258.  
  259.     if (p != NULL) {
  260.         SetPtrSize(p, size);
  261.         if (MemError() != noErr) {
  262.             p2 = xalloc(size);
  263.             memcpy(p2, p, GetPtrSize(p));
  264.             xfree(p);
  265.             p = p2;
  266.         }
  267.     } else {
  268.         p = xalloc(size);
  269.     }
  270.     return p;
  271. }
  272.